import Request from '@utils/request';

const request = new Request('/api/product/');

/**
 * 树结构
 */
export const goodsCategoryGetTree = () => {
  const conv = (cs, level = 0) => {
    if (!cs || level == 2) return cs?.map(a => ({ ...a, level }));
    var list = cs.map(a => ({ ...a, level, children: conv(a.children, level + 1) })).filter(a => a.children && a.children.length > 0);

    if (level == 0 && list.length == 1) {
      return list[0].children;
    }
    return list;
  };
  return Promise.singleCache('goods/category', () => request.get(`goods/category/GetTree`).then(r => conv(r)));
};

/**
 * 分页
 */
export const goodsGoodsUserList = body => {
  return request.post(`goods/userList`, body);
};

/**
 * 详情
 */
export const goodsGetDetail = params => {
  return request.get(`Goods/GetDetail`, params);
};

/**
 * 回显
 */
export const goodsEcho = body => {
  return request.post(`Goods/Echo`, body);
};

/**
 * 品牌
 */
export const goodsBrandInfo = params => {
  return request.get(`goods/brand/Info`, params);
};

/**
 * 下拉列表
 */
export const goodsBrandOptions = () => {
  return request.get(`goods/brand/Options`);
};
/**
 *
 */
export const cartList = () => {
  return request.get(`Cart/List`);
};

/**
 * 添加购物车
 */
export const cartAdd = body => {
  return request.post(`Cart/Add`, body);
};

export const cartCount = () => {
  return request.get(`Cart/Count`);
};

/**
 * 修改购物车
 */
export const cartUpdate = body => {
  return request.post(`Cart/Update`, body);
};

/**
 *
 */
export const cartDelete = body => {
  return request.post(`Cart/Delete`, body);
};

/**
 * 历史商品分页查询
 */
export const collectsHistoryList = body => {
  return request.post(`Collects/HistoryList`, body);
};

/**
 * 收藏商品分页查询
 */
export const collectsList = body => {
  return request.post(`Collects/List`, body);
};

/**
 * 添加收藏
 */
export const collectsAdd = body => {
  return request.post(`Collects/Add`, body);
};

/**
 * 益处收藏
 */
export const collectsRemove = body => {
  return request.post(`Collects/Remove`, body);
};

export const collectsActive = body => {
  return request.post(`Collects/Active`, body);
};


export const hotKeys = params => {
  return request.get(`keywords/HotKeys`, params);
};
